$(function() {

	/*
	 	----------	新建订单	-------------
	 */

	
	/*
			页面初始化
	 */ 
	
	$("#provincespan").load("/area/province!getProvince.action");
	$("#orderForm tr:eq(1)").hide();
	
	/*
	  		表单验证
	*/ 
	$(":input[name='price']").toggle(
			function() {$(this).keydown(validateKey($(this)));},
			function() {$(this).keyup(validateKey($(this)));}
	);
	$(":input[name='quantity']").toggle(
			function() {$(this).focus(validateKey($(this)));},
			function() {$(this).blur(validateKey($(this)));}
	);
	$(":input[name='amount']").toggle(
			function() {$(this).keydown(validateKey($(this)));},
			function() {$(this).keyup(validateKey($(this)));}
	);
	
	// 店铺选择加载店铺信息
	$("#outletsSelect").change(function() {
		var outletsid = $(this).val();
		if (outletsid != "" && outletsid != null) {
			var url1 = "/OutletsAction!getInfo.action?outletsid=";
			$("#customerDetail").load(url1 + outletsid);
		} else {
			$("#customerDetail").html("");
		}
	});

	// 数量变化改变金额
	$("#orderForm input[name='quantity']").blur(
		function() {
			var quan = $(this).val();
			var cur_tr_index = $(this).parent().parent().index("#orderForm tr") - 1;
			var price = $("input[name='price']:eq(" + cur_tr_index + ")").val();
			if (price > 0) {
				var amount = quan * price;
				$("input[name='amounts']:eq(" + cur_tr_index + ")").val(amount);
			}
	});
});


	/*
			选择收货人弹出框
	*/ 
	var url;
	var dialogOption = {
		height : 300,
		width : 880,
		modal : true,
		position : [ 0, 50 ]
	};
	var dialogOption1 = {
		height : 500,
		width : 580,
		modal : true,
		position : [ 0, 50 ]
	};

	function selectCustomer() {
		jQuery("#selectCustomer").dialog(dialogOption1);
		jQuery("#selectCustomer").dialog("open");
	}

//	
	function customerSelected(f) {
		var buttonValue = $(f).val();
		var selectValue = $("#outletsSelect").val();
		if (buttonValue == "确定") {
			if (selectValue != null && selectValue != "") {
				$("#hCustomerid").val(selectValue);
				jQuery("#selectCustomer").dialog("close");
				$("#addCustomButton").html("<label>收货人:"
					+ $("#cusName").html()+ "</label>"
					+ "<input type='button'style='margin:10px;' value='变更收货人' class='button'"
					+ "onclick='selectCustomer();'  >");
				} else {
					alert("请选择一个联系人!!");
				}
			} else {
				jQuery("#selectCustomer").dialog("close");
			}
		}

	// 新建联系人
	function createCustomer() {
		jQuery("#selectCustomer").dialog("close");
		jQuery("#createCustomer").dialog(dialogOption);
		jQuery("#createCustomer").dialog("open");
	}

	function addColume() {

		var xx = $("#orderForm tr:eq(2)").html();
		var addtr_index = $("#orderForm tr").size() - 2;
		var addtr_index1 = addtr_index + 1;
		$("#orderForm tr:eq(" + addtr_index + ")").after("<tr>" + xx + "</tr>");
		$("#orderForm tr:eq(" + addtr_index1 + ") td:eq(0)").html(addtr_index);
}

function loadCity(f) {
	var provinceid = $(f).val();
	$("#cityspan").load(
			"/area/province!getCity.action?provinceid='" + provinceid + "'");
	$("#regionspan").html("");
	$("#managerSelect").html("");
	if (provinceid != 0) {
		$("#managerSelect").load("/OutletsAction!listByArea.action?provinceid=" + provinceid+ "");
	}
}

function loadRegion(f) {
	var cityid = $(f).val();
	$("#regionspan").load(
			"/area/province!getRegion.action?cityid='" + cityid + "'");
	$("#psel").attr("name", "");
	$("#managerSelect").html("");
	if (cityid != 0) {
		$("#managerSelect").load("/OutletsAction!listByArea.action?provinceid=" + cityid + "");
	}
}

function zz(f) {
	var regionid = $(f).val();
	if (regionid != 0) {
		$("#managerSelect").html("");
		$("#managerSelect").load("/OutletsAction!listByArea.action?provinceid=" + regionid + "");
	}
	$("#csel").attr("name", "");
}

// 加载店铺信息
function getOutletsInfo(f) {
	var outletsid = $(f).val();
	if (outletsid != "" && outletsid != null) {
		var url1 = "/OutletsAction!getInfo.action?outletsid=";
		$("#customerDetail").load(url1 + outletsid);
	} else {
		$("#customerDetail").html("");
	}
}
// 验证是否选择收货人
function validate(form) {
	var flag=false;
	if ($("#hCustomerid").val() != "") {
		flag=true;
	} else {
		alert("请先添加收货人！");
	}
	return flag;
}

// 加载产品信息
function loadProductInfo(pselect) {
	var pid = $(pselect).val();
	$.getJSON("/ajax/getProductInfo.action?productid=" + pid, function(data) {
		$("#_productInfo").html(data.name + "  库存总数" + data.curQuantity + "" + "" + data.unit
						+ "  单价:" + data.purchasePrice);
	});
}

// 验证表单
function validateKey(tags){
	$(tags).val($(tags).val().replace(/[^\d.]/g, ""));
	$(tags).val($(tags).val().replace(/^\./g, ""));
	$(tags).val($(tags).val().replace(/\.{2,}/g, "."));
	$(tags).val($(tags).val().replace(".", "$#$").replace(/\./g, "").replace("$#$", "."));
}
